# coding=utf8
import time

import RPi.GPIO as GPIO

from config import Config

'''
微动开关
'''


class MicroSwith:
    def __init__(self, gpio):
        self.gpio = int(gpio)
        GPIO.setmode(GPIO.BCM)
        GPIO.setwarnings(False)
        GPIO.setup(self.gpio, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)  # 微动信号

    def checkOnOrOff(self):
        '''
        监测开关信号，按下后信号返回1，否则返回0
        :return:
        '''
        if GPIO.input(self.gpio) == True:
            return 1
        else:
            return 0


if __name__ == '__main__':
    try:
        gpio = Config().read("MicroSwith", "MicroSwith_pin01")
        microSwith = MicroSwith(gpio)
        while True:
            if microSwith.checkOnOrOff() == 1:
                print('on....')
                time.sleep(1)
            else:
                print('off')
                time.sleep(1)
    except KeyboardInterrupt:  # 如果Ctrl+C中断脚本，则清除GPIO输出
        GPIO.cleanup()
